package com.example.web.service.impl;

import com.example.web.entity.Menu;
import com.example.web.entity.MenuTree;
import com.example.web.mapper.MenuMapper;
import com.example.web.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜单服务实现类
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.selectAllMenus();
    }

    @Override
    public List<MenuTree> getMenuTree() {
        List<Menu> menus = menuMapper.selectAllMenus();
        return buildMenuTree(menus, 0L);
    }

    @Override
    public Menu getMenuById(Long id) {
        return menuMapper.selectMenuById(id);
    }

    @Override
    public List<Menu> getMenusByRoleId(Long roleId) {
        return menuMapper.selectMenusByRoleId(roleId);
    }

    @Override
    public List<MenuTree> getMenuTreeByRoleId(Long roleId) {
        List<Menu> menus = menuMapper.selectMenusByRoleId(roleId);
        return buildMenuTree(menus, 0L);
    }
    
    @Override
    public List<Menu> getMenusByUserId(Long userId) {
        return menuMapper.selectMenusByUserId(userId);
    }

    @Override
    public List<MenuTree> getMenuTreeByUserId(Long userId) {
        List<Menu> menus = menuMapper.selectMenusByUserId(userId);
        return buildMenuTree(menus, 0L);
    }

    @Override
    public List<String> getPermissionsByUserId(Long userId) {
        return menuMapper.selectPermissionsByUserId(userId);
    }

    @Override
    public int addMenu(Menu menu) {
        return menuMapper.insertMenu(menu);
    }

    @Override
    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    @Override
    public int deleteMenuById(Long id) {
        return menuMapper.deleteMenuById(id);
    }

    @Override
    public int deleteMenuByIds(Long[] ids) {
        return menuMapper.deleteMenuByIds(ids);
    }

    /**
     * 构建菜单树
     * @param menus 菜单列表
     * @param parentId 父菜单ID
     * @return 菜单树
     */
    private List<MenuTree> buildMenuTree(List<Menu> menus, Long parentId) {
        List<MenuTree> trees = new ArrayList<>();
        
        // 将菜单列表转换为MenuTree对象
        List<MenuTree> menuTrees = menus.stream().map(menu -> {
            MenuTree tree = new MenuTree();
            BeanUtils.copyProperties(menu, tree);
            return tree;
        }).collect(Collectors.toList());
        
        // 构建菜单树
        for (MenuTree menuTree : menuTrees) {
            if (parentId.equals(menuTree.getParentId())) {
                trees.add(menuTree);
                // 递归构建子菜单
                buildChildMenuTree(menuTrees, menuTree);
            }
        }
        
        return trees;
    }

    /**
     * 递归构建子菜单树
     * @param menuTrees 菜单树列表
     * @param parentTree 父菜单树
     */
    private void buildChildMenuTree(List<MenuTree> menuTrees, MenuTree parentTree) {
        for (MenuTree menuTree : menuTrees) {
            if (parentTree.getId().equals(menuTree.getParentId())) {
                parentTree.addChild(menuTree);
                // 递归构建子菜单
                buildChildMenuTree(menuTrees, menuTree);
            }
        }
    }
}